
function [ES0,EQ0,S0,EQU0] = surplus0(gamma,B,D,P,xi,n,options,cmax,save_eqL,save_eqU)

N = sum(n);
NS = size(xi,1);

% PRI-PVEM joint surplus with independent candidates
ES0 = zeros(N,NS);
if save_eqL == 1
    EQ0 = zeros(N,5,NS);
    S0 = zeros(N,5,NS);
end
if save_eqU == 1
    EQU0 = zeros(N,5,NS);
end

for t = 1:N
    Xt = [blkdiag(kron(eye(2),[1,0,0,D(t,:)]),kron(eye(2),[1,0,D(t,:)]),...
        [1,0,0,D(t,:)]),log(P(t,:)')];
    parfor ns = 1:NS
        if save_eqU == 1
            [eqL,~,eqU,~] = EQ_bounds(gamma,B,Xt,xi(ns,:)',options,cmax,0); % largest and smallest spending equilibria
        else
            eqL = EQ_bounds(gamma,B,Xt,xi(ns,:)',options,cmax,0);
        end
        [~,s] = Shares([B;0;0;xi(ns,:)'],zeros(5,1),[Xt,eqL,eqL.^2],0,ones(5,5),n,zeros(1,2),1);
        ESt(ns) = gamma(3) * log(s(3)) - eqL(3) + gamma(4) * log(s(4)) - eqL(4);
        if save_eqL == 1
            EQt(:,ns) = eqL;
            St(:,ns) = s;
        end
        if save_eqU == 1
            EQUt(:,ns) = eqU;
        end
    end
    ES0(t,:) = ESt;
    if save_eqL == 1
        EQ0(t,:,:) = EQt;
        S0(t,:,:) = St;
    end
    if save_eqU == 1
        EQU0(t,:,:) = EQUt;
    end
end



